/*
* $Id: SettingForm.java 276 2011-02-28 22:15:38Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui;

import apiary.Apiary;
import apiary.component.settings.*;
import apiary.settings.SettingStore;

import java.util.Vector;
import javax.microedition.lcdui.*;

public class SettingForm extends BackScreen implements CommandListener,
                                                       ItemStateListener,
                                                       ItemEventListener
{
    private Vector items;
    private Vector settings;
    private CategoryItem curSel;

    public SettingForm() {
        super("");
        Init();
    }

    public SettingForm(String name) {
        super(name);
        Init();
    }

    public void commandAction(Command c, Displayable d) {
        Apiary apiary = Apiary.getInstance();
        if(c.getCommandType() == Command.BACK) {
            OnOk();
            apiary.Display(backForm);
        } else if (c.getCommandType() == Command.SCREEN)
            DisplayItem();
    }

    public void itemStateChanged(Item item) {
        CategoryItem category = (CategoryItem)item;
        if(category.getActive())
            SetCurSel((CategoryItem)item);
    }

    public void itemNotifyEvent(Item item, String event) {
        if(event.equals(ItemEventListener.PRESS)){
            SetCurSel((CategoryItem)item);
            DisplayItem();
        }
    }

    protected void SetCurSel(CategoryItem item){
        if(item == curSel)
            return;
        curSel = item;
    }

    protected void OnOk() {
        SettingStore.getInstance().saveData();
    }

    protected void DisplayItem() {
        if(curSel.getState().equals(SettingsItem.STATE_UNSET)){
            curSel.setState(SettingsItem.STATE_SET);
            if(curSel.getId().equals(SettingsItem.ID_PARENT))
                AddCategories(curSel);
            else
                SettingStore.getInstance().setData(Integer.valueOf(curSel.getId()),
                                                   Boolean.TRUE);
        }else{
            curSel.setState(SettingsItem.STATE_UNSET);
            if(curSel.getId().equals(SettingsItem.ID_PARENT))
                DeleteCategories(curSel);
            else
                SettingStore.getInstance().setData(Integer.valueOf(curSel.getId()),
                                                   Boolean.FALSE);
        }
        curSel.setImage(SettingsItem.GetImageItem(curSel.getId(),
                curSel.getState().equals(SettingsItem.STATE_SET)));
    }

    private void AddMainCategories(){
        for(int i = 0; i < settings.size(); i++){
            SettingsItem item = (SettingsItem) settings.elementAt(i);
            insert(i, item.getMainCategory());
            items.insertElementAt(item.getMainCategory(), i);
        }
    }
    
    private void AddCategories(CategoryItem mainCategory) {
        int index = items.indexOf(mainCategory);
        SettingsItem item = null;
        for(int i = 0; i < settings.size(); i++){
            item = (SettingsItem) settings.elementAt(i);
            if(item.getMainCategory() == mainCategory)
                break;
        }
        Vector settingsCategory = null;
        if(item != null)
            settingsCategory = item.getSettingCategories();
        for(int i = 0;
            settingsCategory != null && i < settingsCategory.size();
            i++){
            CategoryItem category = (CategoryItem) settingsCategory.elementAt(i);
            insert(index + 1 + i, category);
            items.insertElementAt(category, index + 1 + i);
        }
    }

    private void DeleteCategories(CategoryItem category) {
        int index = items.indexOf(category) + 1;
        if(index == items.size())
            return;
        CategoryItem item = (CategoryItem) items.elementAt(index);
        String id = item.getId();
        item = (CategoryItem) items.elementAt(index);
        id = item.getId();
        while(!id.equals(SettingsItem.ID_PARENT)){
            delete(index);
            items.removeElementAt(index);
            if(index == items.size())
                break;
            item = (CategoryItem) items.elementAt(index);
            id = item.getId();
        }
    }

    private void Init() {
        Apiary apiary = Apiary.getInstance();
        curSel = null;

        setCommandListener(this);
        setItemStateListener(this);

        addCommand(new Command(apiary.getLocaleString("Save"), Command.BACK, 0));
        addCommand(new Command(apiary.getLocaleString("Select"), Command.SCREEN, 0));

        items = new Vector();
        settings = new Vector();

        settings.addElement(new ViewPointsItems(this));
        settings.addElement(new ViewFamiliesItems(this));
        settings.addElement(new ViewInspectItems(this));

        AddMainCategories();
    }
}
